#include <stdio.h>
#include <stdlib.h>

int main() {
    int* arr = (int*)calloc(10, 4);
    for (int i = 0; i < 10; i++) {
        arr[i] = i;
    }

    // 小扩容
    int* arr1 = (int*)realloc(arr, 20 * 4);
    for (int i = 0; i < 20; i++) {
        printf("%d ", arr1[i]);
    }
    printf("\n");
    printf(" %p %p\n", arr, arr1);
    arr[2] = 99;  // 一改都改
    for (int i = 0; i < 20; i++) {
        printf("%d ", arr1[i]);
    }
    printf("\n");

    // 大扩容
    int* arr1 = (int*)realloc(arr, 1000 * 4);
    for (int i = 0; i < 1000; i++) {
        printf("%d ", arr1[i]);
    }
    printf("\n");
    printf(" %p %p\n", arr, arr1);
    for (int i = 0; i < 10; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    // 缩容
    int* arr1 = (int*)realloc(arr, 5 * 4);
    arr1[1] = 666;
    for (int i = 0; i < 10; i++) {
        printf("%d ", arr1[i]);
    }
    printf("\n");
    for (int i = 0; i < 10; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");  // 缩容之后，多余的部分会被释放掉
    
    return 0;
}
